package com.nitramf.catchmystream.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import org.hibernate.search.annotations.DateBridge;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;

/**
 * Represent an item
 * @author martinfilliau
 */
@Entity
@Indexed(index="items")
@NamedQueries({
    @NamedQuery(name=Item.QUERY_FIND_BY_URI, query="SELECT i FROM Item AS i WHERE i.uri = :uri")
})
public class Item extends BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @DocumentId
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Basic
    @Column(length=1000)
    @Field(index=Index.UN_TOKENIZED, store=Store.YES)
    private String uri;

    @Basic
    @Column(length=1000)
    @Field(index=Index.TOKENIZED, store=Store.YES)
    private String title;

    @Lob
    @Field(index=Index.TOKENIZED, store=Store.NO)
    private String description;

    @Basic
    @Field(index=Index.TOKENIZED, store=Store.YES)
    private String author;
    
    @Temporal(value = javax.persistence.TemporalType.DATE)
    @Field(index=Index.UN_TOKENIZED, store=Store.YES)
    @DateBridge(resolution = Resolution.DAY)
    private Date publishedDate;
    
    @Basic
    @Field(index=Index.UN_TOKENIZED, store=Store.YES)
    private Boolean iAmTheAuthor;

    @ManyToMany
    @IndexedEmbedded
    private List<Keyword> keywords;

    
    /* Queries */

    /**
     * Find an item by its URI
     * @param uri uri of the item to find
     */
    public static final String QUERY_FIND_BY_URI = "Item.findByUri";


    /* GETTERs and SETTERs */

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Date getPublishedDate() {
        return publishedDate;
    }

    public void setPublishedDate(Date publishedDate) {
        this.publishedDate = publishedDate;
    }

    public Boolean getiAmTheAuthor() {
        return iAmTheAuthor;
    }

    public void setiAmTheAuthor(Boolean iAmTheAuthor) {
        this.iAmTheAuthor = iAmTheAuthor;
    }
    
    public List<Keyword> getKeywords() {
        return keywords;
    }

    public void setKeywords(List<Keyword> keywords) {
        this.keywords = keywords;
    }

    
    /* Overriden methods */

    @Override
    public int hashCode() {
        int hash = 0;
        hash += ( id != null ? id.hashCode() : 0 );
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!( object instanceof Item )) {
            return false;
        }
        Item other = (Item) object;
        if (( this.id == null && other.id != null ) || ( this.id != null && !this.id.equals(other.id) )) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.nitramf.catchmystream.Item[id=" + id + "]";
    }

}
